class MealsController < ApplicationController
  # GET /meals
  # GET /meals.xml
  def index
    @meals = Meal.find(:all)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @meals }
    end
  end

  # GET /meals/1
  # GET /meals/1.xml
  def show
    @meal = Meal.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @meal }
    end
  end

  # GET /meals/new
  # GET /meals/new.xml
  def new
    @meal = Meal.new
    @products = Product.find(:all)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @meal }
    end
  end

  # GET /meals/1/edit
  def edit
    @meal = Meal.find(params[:id])
    @products = Product.find(:all)
  end

  # POST /meals
  # POST /meals.xml
  def create
    params[:meal][:product_ids] ||= []
    @meal = Meal.new(params[:meal])
    @meal.user = current_user
    @products = Product.find(:all)
    respond_to do |format|
      begin
        Meal.transaction do
          @meal.save!
          flash[:notice] = 'Meal was successfully created.'
          format.html { redirect_to(@meal) }
          format.xml  { render :xml => @meal, :status => :created, :location => @meal }
        end
      rescue
        format.html { render :action => "new" }
        format.xml  { render :xml => @meal.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /meals/1
  # PUT /meals/1.xml
  def update
    params[:meal][:product_ids] ||= []
    @meal = Meal.find(params[:id])
    @products = Product.find(:all)
    respond_to do |format|
      begin
        Meal.transaction do
          @meal.update_attributes!(params[:meal])
          flash[:notice] = 'Meal was successfully updated.'
          format.html { redirect_to(@meal) }
          format.xml  { head :ok }
        end
      rescue
        format.html { render :action => "edit" }
        format.xml  { render :xml => @meal.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /meals/1
  # DELETE /meals/1.xml
  def destroy
    @meal = Meal.find(params[:id])
    @meal.destroy

    respond_to do |format|
      format.html { redirect_to(meals_url) }
      format.xml  { head :ok }
    end
  end
  
  def comment
    meal = Meal.find(params[:id])
    comment = meal.meal_comments.create(params[:meal_comment])
    flash[:notice] = "Comment can't be blank" unless comment.valid?
    redirect_to :action => :show, :id => params[:id]
  end
end
